// threadtest.cc 
//	Simple test case for the threads assignment.
//
//	Create two threads, and have them context switch
//	back and forth between themselves by calling Thread::Yield, 
//	to illustratethe inner workings of the thread system.
//
// Copyright (c) 1992-1993 The Regents of the University of California.
// All rights reserved.  See copyright.h for copyright notice and limitation 
// of liability and disclaimer of warranty provisions.

#include "copyright.h"
#include "system.h"
//#include "../machine/machine.h"	//OceanMaster
//#include "../filesys/filesys.h"
//----------------------------------------------------------------------
// SimpleThread
// 	Loop 5 times, yielding the CPU to another ready thread 
//	each iteration.
//
//	"which" is simply a number identifying the thread, for debugging
//	purposes.
//----------------------------------------------------------------------

void
SimpleThread(int which)
{
    int num;
    
    for (num = 0; num < 5; num++) {
	printf("*** thread %d looped %d times\n", which, num);
        currentThread->Yield();
    }
}

//----------------------------------------------------------------------
// ThreadTest
// 	Set up a ping-pong between two threads, by forking a thread 
//	to call SimpleThread, and then calling SimpleThread ourselves.
//----------------------------------------------------------------------

void
ThreadTest()
{
    DEBUG('t', "Entering SimpleTest");

    Thread *t = new Thread("forked thread");

    t->Fork(SimpleThread, 1);
    SimpleThread(0);
}

//-----------------------------By OceanMaster---------------------------
//ListTest
//	Test the List class defined in list.h and list.cc
//	It has been modified by OceanMaster
//----------------------------------------------------------------------
void
ListTest()
{
	DEBUG('t',"Entering ListTest");

	List * list = new List;
	
	list->Append((void *)1);
	list->Append((void *)2);
	list->Append((void *)3);
	list->Append((void *)4);
	list->Append((void *)5);
	list->Append((void *)6);
	list->Append((void *)7);
	list->Append((void *)8);
	list->Append((void *)9);

	printf("List Length: %3d\n",list->Count());
	for(int i=0;i<list->Count();i++)
	{
		printf("element %3d:%5d\n",i,(int)list->Get(i));
	}

	printf("Test index over bound\n");
	printf("index 11:%s\n",list->Get(11)==NULL?"NULL":"ERROR");

	printf("After removing '%d', size is %d\n",(int)list->Remove(),list->Count());
}

/*
void
FileTest()
{
	char memory[4*PageSize+1];
	memory[4*PageSize]='\0';

	fileSystem -> Create("testfile",100*PageSize);

	for(int i=0;i<PageSize;i++)
		memory[i]='A';
	for(int i=0;i<PageSize;i++)
		memory[PageSize+i]='B';
	for(int i=0;i<PageSize;i++)
		memory[3*PageSize+i]='D';

	OpenFile * file = fileSystem -> Open("testfile");
	file -> WriteAt(memory, PageSize, 90*PageSize);
	file -> WriteAt(&memory[PageSize], PageSize, 99*PageSize);
	file -> WriteAt(memory+3*PageSize, PageSize, PageSize);

	file -> ReadAt(memory, PageSize, PageSize);
	file -> ReadAt(&memory[PageSize], PageSize, 99*PageSize);
	file -> ReadAt(memory+3*PageSize, PageSize, 90*PageSize);

	printf("%s\n",memory);
}*/
